//package com.xb.system.listener;
//
//import cn.hutool.core.util.ObjectUtil;
//import com.alibaba.excel.EasyExcel;
//import com.alibaba.excel.ExcelWriter;
//import com.alibaba.excel.context.AnalysisContext;
//import com.alibaba.excel.event.AnalysisEventListener;
//import com.alibaba.excel.write.metadata.WriteSheet;
//import com.xb.model.domain.UsersInfo;
//import com.xb.model.sout.PhoneToWxModel;
//import com.xb.system.service.WxUserService;
//import lombok.extern.slf4j.Slf4j;
//
//import java.nio.file.Files;
//import java.nio.file.Path;
//import java.nio.file.Paths;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.CopyOnWriteArrayList;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import java.util.concurrent.TimeUnit;
//import java.util.concurrent.locks.Lock;
//import java.util.concurrent.locks.ReentrantLock;
//
//@Slf4j
//public class PhoneToWxExcelDataListener extends AnalysisEventListener<PhoneToWxModel> {
//
//    private final ExecutorService executorService;
//    /** 模板 */
//    private final List<PhoneToWxModel> phoneToWxModels = new CopyOnWriteArrayList<>();
//    private final WxUserService wxUserService;
//    private final ExcelWriter excelWriter;
//    private final WriteSheet writeSheet;
//    private final Lock lock = new ReentrantLock();
//    private final int BATCH_SIZE = 1000;
//
//    public PhoneToWxExcelDataListener(WxUserService wxUserService, String outputFilePath) {
//        this.wxUserService = wxUserService;
//
//        // 创建 ExcelWriter 实例和 WriteSheet 实例
//        //获取outputFilePath的目录，但是outputFilePath是一个文件路径，只创建目录不需要文件名
//        Path parent = Paths.get(outputFilePath).getParent();
//        try {
//            Files.createDirectories(parent);
//            this.excelWriter = EasyExcel.write(outputFilePath, PhoneToWxModel.class).build();
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new RuntimeException("创建 ExcelWriter 实例和 WriteSheet 实例失败");
//        }
//        this.writeSheet = EasyExcel.writerSheet("Sheet1").build();
//
//        // 创建固定大小的线程池
//        this.executorService = Executors.newFixedThreadPool(10);
//    }
//
//    /**
//     * 每读一行就会执行这个方法
//     *
//     * @param phoneToWxModel
//     * @param context
//     */
//    @Override
//    public void invoke(PhoneToWxModel phoneToWxModel, AnalysisContext context) {
//        phoneToWxModels.add(phoneToWxModel);
//        if (phoneToWxModels.size() >= BATCH_SIZE) {
//            processCurrentBatch();
//        }
//    }
//
//
//    /**
//     * 所有数据读取完成后执行
//     *
//     * @param context
//     */
//    @Override
//    public void doAfterAllAnalysed(AnalysisContext context) {
//        // 处理剩余的批次
//        if (!phoneToWxModels.isEmpty() && phoneToWxModels.size() < BATCH_SIZE) {
//            processCurrentBatch();
//        }
//        executorService.shutdown();
//        try {
//            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
//        } catch (InterruptedException e) {
//            Thread.currentThread().interrupt();
//        }
//        // 处理完成后关闭 ExcelWriter
//        excelWriter.finish();
//    }
//
//    private void processCurrentBatch() {
//        List<PhoneToWxModel> batchToProcess = new ArrayList<>(phoneToWxModels);
//        phoneToWxModels.clear();
//        executorService.submit(() -> processBatch(batchToProcess));
//    }
//
//    private void processBatch(List<PhoneToWxModel> phoneToWxModels) {
//        for (PhoneToWxModel model : phoneToWxModels) {
//            processCompanyData(model);
//        }
//        // 写入数据时加锁
//        lock.lock();
//        try {
//            excelWriter.write(phoneToWxModels, writeSheet);
//        } finally {
//            lock.unlock();
//        }
//    }
//
//    /**
//     * 根据手机获取微信号
//     * @param model
//     */
//    private void processCompanyData(PhoneToWxModel model) {
//        List<UsersInfo> usersInfos = wxUserService.getByPhone(model.getPhone());
//        if (ObjectUtil.isNotEmpty(usersInfos) && usersInfos.size() > 0) {
//            UsersInfo UsersInfo = usersInfos.get(0);
//            model.setWxId(UsersInfo.getWxId());
//        }
//    }
//}